<?php class Contact_m extends CI_Model {

    function __construct()
    {
        // Call the Model constructor
        parent::__construct();
          $this->load->database();
    }
      
    function get_all_contact(){
        $query=$this->db->select()
                        ->from('tbl_contact')
                        ->order_by('id', "DESC")
                        ->get();
        return $query->result_array();    
    }
    
    function get_contact_pagination($per_page, $off_set)
    {
        $this->db->select('tbl_contact.*');
        $this->db->from('tbl_contact');
        $this->db->limit($per_page, $off_set);
        $this->db->order_by('id', 'DESC');
        $query = $this->db->get();
        return $query->result_array();
    }
    
    function get_detail_contact($id)
    {
        $query=$this->db->get_where('tbl_contact', array('id' => $id));
        return $query->row_array();
    }
    
    function delete_contact($id){
       return  $this->db->delete('tbl_contact', array('id' => $id));
    }
    
    function delete_multi_contacts($list){
      $this->db->where('id in '.$list);
      $query=  $this->db->delete('tbl_contact');
      return $query;
    }
    
    function add_contact($data){
        return $this->db->insert('tbl_contact', $data);
    }
           
    function get_contact_information(){
        $query = $this->db->select()
                          ->from('contact_information')
                          ->where("id", 1)
                          ->limit(1)
                          ->get();
        
        if($query->num_rows() <= 0){
            return FALSE;
        }else{
            return $query->row();
        }
    }
    
    function edit_contact_information($contact_data){
        $data = $this->get_contact_information();
        if($data === FALSE){
            $this->db->insert("contact_information", array("id"=>1));
            if($this->db->affected_rows() == 1){
                return $this->db->update("contact_information", $contact_data , array("id"=>1));
            }
            
        }else{
            return $this->db->update("contact_information", $contact_data , array("id"=>1));
        }
    }
}
?>
